#include "SDL.h" 
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <string>

//Screen attributes 
const int SCREEN_WIDTH = 640; 
const int SCREEN_HEIGHT = 480; 
const int SCREEN_BPP = 32;

SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;

SDL_Event event; 

//Loads an images and sets it to the correct bit and image.
SDL_Surface *load_image( std::string filename ) 
{ 
	//The image that's loaded 
	SDL_Surface* loadedImage = NULL; 
	//The optimized image that will be used 
	SDL_Surface* optimizedImage = NULL; 
	//Load the image 
	loadedImage = IMG_Load( filename.c_str() ); 
	//If the image loaded 
	if( loadedImage != NULL ) 
	{ 
		//Create an optimized image 
		optimizedImage = SDL_DisplayFormat( loadedImage ); 
		//Free the old image 
		SDL_FreeSurface( loadedImage );
		//If the image was optimized just fine 
		if( optimizedImage != NULL ) 
		{ 
			//Map the color key 
			Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
			//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent 
			SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey ); 
		}
	}
	//Return the optimized image 
	return optimizedImage; 
}

// Applies a new image to the current image.
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL ) 
{ 
	//Holds offsets 
	SDL_Rect offset; 
	//Get offsets 
	offset.x = x; 
	offset.y = y; 
	//Blit 
	SDL_BlitSurface( source, clip, destination, &offset ); 
}  

// Initialized all the variables.
bool init() 
{ 
	//Initialize all SDL subsystems 
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )  
		return false;  
	//Set up the screen 
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE ); 
	//If there was an error in setting up the screen 
	if( screen == NULL )  
		return false;  
	//Set the window caption 
	SDL_WM_SetCaption( "Tetris", NULL ); 
	//If everything initialized fine 
	return true; 
}

// Loads up all the files going to be used.
bool load_files() 
{ 
	//Load the image 
	background = load_image( "background.png" );
	//If there was an error in loading the image
	if( background == NULL ) 
		return false; 
	//If everything loaded fine 
	return true; 
}

// Cleans up everything and quits the program.
void clean_up() 
{ 
	//Free the surfaces 
	SDL_FreeSurface( background ); 
	//Quit SDL 
	SDL_Quit(); 
} 
int main( int argc, char* args[] ) 
{ 
	bool quit = false; // Boolean to tell if user quit application or not.

	if( init() == false ) // Runs init to initilize all the variables. If something went wrong.
		return 1; // Return 1;

	if( load_files() == false ) // Runs load_files to load up all the files. If something went wrong.
		return 1; // Return 1;

	apply_surface( 0, 0, background, screen );
	if( SDL_Flip( screen ) == -1 ) 
		return 1;  
	while( quit == false )
	{
		if( SDL_PollEvent( &event ) )
		{
			if( event.type == SDL_QUIT )
				quit = true;
		}
	}
	clean_up();
	return 0;
}
